package example;

import commom.ColorAndXY;
import util.Util;
import commom.XY;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Vector;

public class KsRobot {
    static Robot robot;

    static {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws AWTException, InterruptedException, IOException {
        // 启动命令
        // java -jar -Xms128m -Xmx256m -XX:PermSize=128M -XX:MaxPermSize=256M genshin.jar

        // 打包后路径改变 在项目结构中编辑img文件夹输出到jar根目录
        // https://blog.csdn.net/antony1776/article/details/89249509
        // InputStream pngNeedRobot = KsRobot.class.getClassLoader().getResourceAsStream("need-robot-1600x900.png");
        // InputStream pngOkRobot = KsRobot.class.getClassLoader().getResourceAsStream("ok-robot-bg-1600x900.png");
        // InputStream pngNeedRobot = KsRobot.class.getClassLoader().getResourceAsStream("need-robot-1920x1200.png");
        // InputStream pngOkRobot = KsRobot.class.getClassLoader().getResourceAsStream("ok-robot-bg-1920x1200.png");
        // 调试路径位置
        // String pngNeedRobot = "need-robot-1600x900.png"
        // String pngOkRobot = "ok-robot-bg-1600x900.png"
        String pngNeedRobot = "img/need-robot-1920x1200.png";
        String pngOkRobot = "img/ok-robot-bg-1920x1200.png";

        ColorAndXY[] colorAndXIESNeedRobot = Util.readColorAndXYByFilePath(pngNeedRobot);
        ColorAndXY[] colorAndXIESOkRobot = Util.readColorAndXYByFilePath(pngOkRobot);

        Util.printImg(colorAndXIESNeedRobot);
        while (true) {
            for (XY xy : loopFind(colorAndXIESNeedRobot, -1)) {
                System.out.println("开始 人机验证");
                robot.mouseMove(0, 0);
                currentLocationMoveToTarget(xy.getX() - 30, xy.getY());
                // robot.mouseMove(xy.x, xy.y);
                robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                Vector<XY> robotOk = loopFind(colorAndXIESOkRobot, 10);
                if (robotOk.isEmpty())
                    System.out.println("失败 人机验证");
                else
                    System.out.println("OK 人机验证");
                robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            }
        }
    }

    /**
     * 循环查找图片位置
     *
     * @param colorAndXIES 图片信息
     * @param timeToExist  10s后退出查找，-1死循环一直查找
     * @return
     */
    public static Vector<XY> loopFind(ColorAndXY[] colorAndXIES, int timeToExist) throws InterruptedException {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小
        Rectangle screenRectangle = new Rectangle(screenSize);//根据屏幕大小创建一个矩形
        long loopStart = System.currentTimeMillis();
        while (true) {
            BufferedImage screenCapture = robot.createScreenCapture(screenRectangle);
            // ImageIO.write(screenCapture, "png", new File("C:\\Users\\Johnson\\Pictures\\截图debugger.png"));
            long startTime = System.currentTimeMillis();
            Vector<XY> imagePosition = Util.findImagePosition(screenCapture, colorAndXIES);
            // if (!imagePosition.isEmpty())
            System.out.println(imagePosition + " " + (System.currentTimeMillis() - startTime));
            if (!imagePosition.isEmpty() || (timeToExist != -1 && (System.currentTimeMillis() - loopStart) > timeToExist * 1000L))
                return imagePosition;
            Thread.sleep(1000);
        }
    }

    public static void currentLocationMoveToTarget(int x, int y) throws AWTException, InterruptedException {
        Robot robot = new Robot();
        Point currentPoint = MouseInfo.getPointerInfo().getLocation();
        double currentPointX = currentPoint.getX();
        double currentPointY = currentPoint.getY();
        double offsetX = x - currentPointX;
        double offsetY = y - currentPointY;
        double stepX = offsetX / 200;
        double stepY = offsetY / 200;
        for (int i = 0; i < 200; i++) {
            currentPointX += stepX;
            currentPointY += stepY;
            robot.mouseMove((int) currentPointX, (int) currentPointY);
            Thread.sleep(10);
        }
    }
}
